using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using Jonathan.SharpDB;
using System.Drawing;

namespace Jonathan
{
	/// <summary>
	/// A Control that will Display the users book marks.
	/// </summary>
    public class ctrlBookMark : Control
    {
        private OutlookGrid.OutlookGrid _gridBookMarks;
        private frmMain m_FormMain;
        private BookMarkDropDown m_DropDown;

		/// <summary>
		/// Creates a new Book Mark Control.
		/// </summary>
		/// <param name="main">Reference to Main Form.</param>
		/// <param name="dropDown">DropDown Control is attached to.</param>
        public ctrlBookMark (frmMain main, BookMarkDropDown dropDown)
        {
            this.m_FormMain = main;
            this.m_DropDown = dropDown;
            this._gridBookMarks = new Jonathan.OutlookGrid.OutlookGrid();
            this._gridBookMarks.Dock = DockStyle.Fill;
            this.SetupGrid();
            this.Width = 300;
            this.Height = 300;
            this._gridBookMarks.CellClick += new DataGridViewCellEventHandler(_gridBookMarks_CellClick);

            this.Controls.Add(_gridBookMarks);
        }

        /// <summary>
        /// Sets up meta data for the Grid...
        /// </summary>
        private void SetupGrid ()
        {
            this._gridBookMarks.EmptyGridText = "No Book Marks Created.";
            this._gridBookMarks.AllowUserToAddRows = false;
            this._gridBookMarks.AllowUserToDeleteRows = false;
            this._gridBookMarks.AllowUserToResizeColumns = false;
            this._gridBookMarks.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
            this._gridBookMarks.BackgroundColor = System.Drawing.Color.White;
            this._gridBookMarks.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.SingleHorizontal;
            this._gridBookMarks.CollapseIcon = null;
            this._gridBookMarks.ColumnHeadersVisible = false;
            this._gridBookMarks.BorderStyle = BorderStyle.None;
            DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle();
            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.TopLeft;
            dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Window;
            dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.ControlText;
            dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            this._gridBookMarks.DefaultCellStyle = dataGridViewCellStyle1;
            this._gridBookMarks.Dock = System.Windows.Forms.DockStyle.Fill;
            this._gridBookMarks.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically;
            this._gridBookMarks.ExpandIcon = null;
            this._gridBookMarks.GridColor = System.Drawing.Color.LightGray;
            this._gridBookMarks.Location = new System.Drawing.Point(0, 0);
            this._gridBookMarks.Name = "m_SearchGrid";
            this._gridBookMarks.RowHeadersVisible = false;
            this._gridBookMarks.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this._gridBookMarks.Size = new System.Drawing.Size(1861, 1494);
            this._gridBookMarks.TabIndex = 6;
            this._gridBookMarks.ScrollBars = ScrollBars.None;
            this._gridBookMarks.CanCollapse = false;
            //this._gridBookMarks.ExpandIcon = this.m_FormMain.GetInternalImage("plus");
            //this._gridBookMarks.CollapseIcon = this.m_FormMain.GetInternalImage("minus");

            this.AddColumns();
        }

        /// <summary>
        /// Adds the Columns for the Grid...
        /// </summary>
        private void AddColumns ()
        {
            this._gridBookMarks.Columns.Add("colVersion", "Version");
            this._gridBookMarks.Columns.Add("colInfo", "Passage");
            // Quick Lookup
            DataGridViewImageColumn colILookUp = new DataGridViewImageColumn();
            colILookUp.Name = "colLookUp";
            colILookUp.HeaderText = "Look Up";
            colILookUp.Width = 22;
            this._gridBookMarks.Columns.Add(colILookUp);
            // Goto
            DataGridViewImageColumn colIGoto = new DataGridViewImageColumn();
            colIGoto.Name = "colGoto";
            colIGoto.HeaderText = "Goto";
            colIGoto.Width = 16;
            this._gridBookMarks.Columns.Add(colIGoto);
            // Remove
            DataGridViewImageColumn colIRemove = new DataGridViewImageColumn();
            colIRemove.Name = "colRemove";
            colIRemove.HeaderText = "Remove";
            colIRemove.Width = 16;
            this._gridBookMarks.Columns.Add(colIRemove);

            this._gridBookMarks.GroupTemplate.Column = this._gridBookMarks.Columns[0];
            this._gridBookMarks.Columns[0].Visible = false;
            this._gridBookMarks.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }

		/// <summary>
		/// Occurs when a cell in the bookmarks grid is clicked.
		/// </summary>
		/// <param name="sender">Cell Clicked</param>
		/// <param name="e">DataGridViewCellEventArgs</param>
        void _gridBookMarks_CellClick (object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                this.m_DropDown.CloseDropDown();
                List<cPassage> passages = new List<cPassage>();
                passages.Add((cPassage)this._gridBookMarks.Rows[e.RowIndex].Tag);
                this.m_FormMain.StartBible(passages, null);
            }
            else if (e.ColumnIndex == 3)
            {
                cPassage passage = (cPassage)this._gridBookMarks.Rows[e.RowIndex].Tag;
                string dbPath = this.m_FormMain.GetDBPath(passage.Version);
                if (!string.IsNullOrEmpty(dbPath))
                {
                    List<cPassage> passages = new List<cPassage>();
                    passages.Add(passage);
                    Database db = new Database(dbPath);
                    StringBuilder sb = Common.GetToolTipText(passages, passage.Version, db);

                    fHtmlToolTip tt = new fHtmlToolTip(this.m_FormMain, this._gridBookMarks.PointToScreen(
                        new Point(this._gridBookMarks.Right + 3, this._gridBookMarks.Rows[e.RowIndex].Height * this._gridBookMarks.Rows[e.RowIndex].Index)),
                        sb.ToString(), passage.ToString(), true);
                    tt.ShowTT();
                }
            }
            else if (e.ColumnIndex == 4)
            {
                this.m_DropDown.CloseDropDown();
                this.m_FormMain.RemoveBookMark((cPassage)this._gridBookMarks.Rows[e.RowIndex].Tag);
            }

            this._gridBookMarks.ClearSelection();
        }

		/// <summary>
		/// Occurs when the background color changes, set the grid to the controls background color.
		/// </summary>
		/// <param name="e">Event Arguments</param>
        protected override void OnBackColorChanged (EventArgs e)
        {
            base.OnBackColorChanged(e);
            this._gridBookMarks.BackgroundColor = this.BackColor;
        }

        /// <summary>
        /// Adds the given Book Mark...
        /// </summary>
        /// <param name="bm">Book Mark to Add.</param>
        public void AddBookMark (cPassage bm)
        {
            OutlookGrid.OutlookGridRow row = new Jonathan.OutlookGrid.OutlookGridRow();
            row.CreateCells(this._gridBookMarks, new object[] { bm.Version, bm.Book + " " + bm.Chapter + ":" + bm.Verse, 
                global::Jonathan.InternalImages.gotoBookMark, global::Jonathan.InternalImages.chapter, global::Jonathan.InternalImages.DeleteHS });
            row.Tag = bm;
            row.Cells[2].ToolTipText = "Goto Book Mark";
            row.Cells[3].ToolTipText = "Look up Book Mark";
            row.Cells[4].ToolTipText = "Delete Book Mark";
            this._gridBookMarks.Rows.Add(row);
        }

        /// <summary>
        /// Sorts the Grid...
        /// </summary>
        public void SortGrid ()
        {
            if (this._gridBookMarks.Rows.Count > 0)
            {
                this._gridBookMarks.Sort(new PassageGridComparer(ListSortDirection.Ascending,
                    this.m_FormMain.GetDBPath(this._gridBookMarks.Rows[0].Cells[0].Value.ToString())));

                int rowHeight = this._gridBookMarks.Rows[0].Height;
                if ((rowHeight * this._gridBookMarks.Rows.Count) > 300)
                {
                    this._gridBookMarks.ScrollBars = ScrollBars.Vertical;
                    this._gridBookMarks.Height = 300;
                    this.Height = 300;
                }
                else
                {
                    this._gridBookMarks.ScrollBars = ScrollBars.None;
                    this._gridBookMarks.Height = (this._gridBookMarks.Rows.Count + 1) * rowHeight;
                    this.Height = this._gridBookMarks.Rows.Count * rowHeight;
                }
            }
        }

        /// <summary>
        /// Clears the information in the grid (re-adds the columns)...
        /// </summary>
        public void ClearGrid ()
        {
            this._gridBookMarks.BindData(null, null);
            this._gridBookMarks.ClearGroups();
            this._gridBookMarks.Rows.Clear();
            this.AddColumns();
        }

        /// <summary>
        /// Gets or Sets the OutlookGrid...
        /// </summary>
        public OutlookGrid.OutlookGrid Grid
        {
            get { return this._gridBookMarks; }
            set { this._gridBookMarks = value; }
        }
    }
}
